# -*- coding: utf-8 -*-


'''eof
name:企业未来6个月未结清到期贷款余额比月均申报收入总额
code:EC07_Later6MonsUpaidLoanBalToTaxMonthlyIncomePro
tableName:
columnName:
groups:企业征信
dependencies:EC_DSJ
type:常用指标
dataSourceType:
eof'''


import sys
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if len(table) == 0:
            return [{}]
        elif table is None or isinstance(table, list) is False:
            return None
        else:
            return data.get(tableName)
def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False




def EC_amount():
    data=EC_DSJ['data']
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        ##企业征信 【基本信息段】(ED01A)
        table1=getTable(data,"EDA",1)
        if table1 is None or table1 =="":
            return  0
        elif table1==[{}]:
            return 0
        else:
            table2=getTable(table1[0],"ED01",0)
            if table2 is None or table1 =="":
                return 0
            elif table2 ==[{}]:
                return 0
            else:
                factor=[]
                for i in table2:
                    ED01A=getTable(i,"ED01A",0)
                    if  ED01A in([{}],None):
                        factor.append(None)
                    elif ED01A[0].has_key("ED01AD01") and ED01A[0].get("ED01AD01")=="1" :
                        if  ED01A[0].has_key("ED01AJ01") and isfloat(ED01A[0].get("ED01AJ01"))is True :
                            amount = float(ED01A[0].get("ED01AJ01"))
                            factor.append(amount)
                        else:
                            factor.append(0)
                    else:
                        factor.append(0)
                else:
                    factor.append(None)
                factorNotNone = filter(lambda x: x is not None, factor)
                if len(factorNotNone) == 0:
                    return None
                else:
                    return sum(factorNotNone)
    except:
        return 0





result = EC_amount()


